#include "guahao.h"
#include "ui_guahao.h"
#include "widget.h"
#include <QMessageBox>
#include "network.h"
#include "MessageType.h"
#include "tcpsocketsingleton.h"
#include <QVBoxLayout>


guahao::guahao(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::guahao)
{
    ui->setupUi(this);

    this->setWindowTitle("医生端");
    this->setWindowIcon(QIcon(":doctorimg/img/doctorIcon.png"));

    // 设置主窗口背景及控件样式
    this->setStyleSheet(
        "QWidget { background-color: rgba(255, 240, 245, 0.95); }" // 整体背景为浅粉色
        "QLabel { color: #880E4F; font-size: 24px; font-weight: bold; }" // 增大字体为24px
        "QLineEdit { min-height: 40px; background-color: rgba(255, 255, 255, 0.9); "
        "border: 2px solid #F48FB1; padding: 12px; border-radius: 10px; font-size: 20px; color: black; }" // 调整输入框
        "QPushButton { min-width: 100px; min-height: 40px; background-color: #F8BBD0; color: white; border: 2px solid #F48FB1; "
        "padding: 15px 30px; border-radius: 12px; font-size: 22px; font-weight: bold; }" // 按钮背景、字体及大小设置
        "QPushButton:hover { background-color: #F48FB1; }" // 悬停时按钮背景变深粉色
    );

    // 垂直布局设置
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setAlignment(Qt::AlignCenter); // 垂直布局居中
    mainLayout->setSpacing(20); // 控件间距

    // 添加标题标签
    QLabel *titleLabel = ui->label;
    mainLayout->addWidget(titleLabel, 0, Qt::AlignCenter); // 将标题标签添加到主布局并居中

    // 创建网格布局用于医生姓名和医生ID
    QGridLayout *gridLayout = new QGridLayout();
    gridLayout->addWidget(ui->label_2, 0, 0); // 医生姓名标签
    gridLayout->addWidget(ui->lineEdit_name, 0, 1); // 医生姓名输入框
    gridLayout->addWidget(ui->label_3, 1, 0); // 医生ID标签
    gridLayout->addWidget(ui->lineEdit_ID, 1, 1); // 医生ID输入框
    mainLayout->addLayout(gridLayout); // 将网格布局添加到主布局

    // 添加列表控件
    mainLayout->addWidget(ui->listWidget); // 将列表控件添加到主布局

    // 创建水平布局用于按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(ui->refreshButton); // 添加刷新按钮
    buttonLayout->addWidget(ui->return_2); // 添加退出按钮
    mainLayout->addLayout(buttonLayout); // 将按钮布局添加到主布局

    // 设置主布局
    this->setLayout(mainLayout);

    // 为刷新按钮设置快捷键 Enter
    ui->refreshButton->setShortcut(Qt::Key_Enter);

    tcpSocket = TcpSocketSingleton::instance().socket();

    // tcpSocket  = new QTcpSocket;
    // // tcpSocket->connectToHost("127.0.0.1", 12345);
    // if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
    //     tcpSocket->connectToHost("127.0.0.1", 12345);
    // }

     // 断开之前所有连接
     disconnect(tcpSocket, &QTcpSocket::readyRead, nullptr, nullptr);
     tcpSocket->flush();

    // 连接信号槽，处理服务器的响应
    connect(tcpSocket, &QTcpSocket::readyRead, this, [ this ]() {
        // 读取服务器返回的字节数组
        qDebug() << "处理服务器挂号的响应";
        QByteArray responseData = tcpSocket->readAll();
        QList<QMap<QString, QString >> responseMap = convertByteArrayToQMapArrayCustom(responseData); // 假设此函数将字节数组转换为 QList<QMap>

        // 清空界面显示
        ui->listWidget->clear();

        // 遍历 QList 并将每个 QMap 的内容显示到界面
        for (const QMap<QString, QString>& map : responseMap) {
            QString itemText;
            for (auto it = map.constBegin(); it != map.constEnd(); ++it) {
                itemText += it.key() + ": " + it.value() + "\n";
            }
            ui->listWidget->addItem(itemText.trimmed()); // 去掉多余换行，添加到 listWidget
        }
    });

    // loadData(); // 初始加载数据
}

guahao::~guahao()
{
    delete ui;
}

void guahao::loadData()
{
    QString name = ui->lineEdit_name->text();
    QString id = ui->lineEdit_ID->text();

    // 向服务器发送请求获取挂号信息
    QMap<QString, QString> data;
    data.insert("type", REGISTRATIONDATABASE); // 设置数据类型
    data.insert("databaseoperator", QString::number(SELECT)); // 设置操作类型为查询
    data.insert("doctorid", id);
    data.insert("doctorname", name);
    // 将数据转换为 QByteArray 并发送
    QByteArray sendData = mapToByteArrayCustom(data);
    tcpSocket->write(sendData);
    // QMessageBox::information(this, "挂号", "查找挂号信息已发送");
}

void guahao::on_return_2_clicked()
{
    this->close();
    // Widget *widget = new Widget();
    // widget->show();
}

void guahao::on_refreshButton_clicked()
{
    loadData(); // 刷新并重新加载数据
}
